package com.wr.recruit.service.apis;

import com.google.common.collect.Lists;
import com.wr.recruit.service.apis.dto.UserDto;
import com.wr.recruit.service.apis.dto.UserGrantDto;
import com.wr.recruit.service.dao.dbo.UserDbo;
import com.wr.recruit.service.service.UserService;
import com.wr.recruit.service.utils.APIResponse;
import com.wr.recruit.service.utils.APIResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static com.wr.recruit.service.utils.Constants.TRUE;

@Controller
public class UserApis {

    private UserService userService;

    @Autowired
    public UserApis(UserService userService) {
        this.userService = userService;
    }

    @PostMapping(value = "/user/create", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserCreate(@RequestBody UserDto userDto){
        if (Objects.isNull(userDto.getUserName())
                || Objects.isNull(userDto.getPassword())
                || Objects.isNull(userDto.getTrueName()))
            return new APIResponseSet<>(false, "缺少必填参数");
        UserDbo userDbo = new UserDbo();
        userDbo.setUserName(userDto.getUserName());
        userDbo.setPassword(userDto.getPassword());
        userDbo.setTrueName(userDto.getTrueName());
        userDbo.setCompanyCode(Objects.isNull(userDto.getCompanyCode())?null:userDto.getCompanyCode());
        userDbo.setPhone(Objects.isNull(userDto.getPhone())?"":userDto.getPhone());
        userDbo.setMail(Objects.isNull(userDto.getMail())?"":userDto.getMail());
        UserDbo userInfo = userService.createUserInfo(userDbo);

        if (Objects.isNull(userInfo))
            return new APIResponseSet<>(false, "创建失败，请检查是否已创建或公司代码错误");
        return new APIResponseSet<>(true, "创建用户成功" , Lists.newArrayList(userInfo));
    }

    @PostMapping(value = "/user/update", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserUpdate(@RequestBody UserDto userDto){
        UserDbo userDbo = new UserDbo();
        if (Objects.isNull(userDto.getUserName()))
            return new APIResponseSet<>(false, "更新用户失败");
        userDbo.setUserName(userDto.getUserName());
        if (!Objects.isNull(userDto.getPassword()))
            userDbo.setPassword(userDto.getPassword());
        if (!Objects.isNull(userDto.getTrueName()))
            userDbo.setTrueName(userDto.getTrueName());
        if (!Objects.isNull(userDto.getPhone()))
            userDbo.setPhone(userDto.getPhone());
        if (!Objects.isNull(userDto.getMail()))
            userDbo.setMail(userDto.getMail());
        if (!Objects.isNull(userDto.getStatus()))
            userDbo.setStatus(TRUE.equals(userDto.getStatus()));

        UserDbo user = userService.updateUserInfo(userDbo);

        if (Objects.isNull(user))
            return new APIResponseSet<>(true, "更新用户失败");
        return new APIResponseSet<>(true, "更新用户成功" , Lists.newArrayList(user));
    }

    @PostMapping(value = "/user/delete", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserDelete(@RequestBody UserDto userDto){
        userService.revokeUserInfo(userDto.getId());
        return new APIResponseSet<>(true, "删除用户成功");
    }

    @GetMapping(value = "/user", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserListGet(@RequestParam(value = "userName",required = false) String userName,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam(value = "pageNum") Integer pageNum){
        Page<UserDbo> userListByUserName = userService.getUserListByUserName(userName, pageSize, pageNum);
        return new APIResponseSet<>(true, "获取用户成功" , Lists.newArrayList(userListByUserName));
    }

    @GetMapping(value = "/user/{userName}", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserGet(@PathVariable("userName") String userName){
        UserDbo userInfo = userService.getUserInfo(userName);
        return new APIResponseSet<>(true, "获取用户成功" , Lists.newArrayList(userInfo));
    }

    @PostMapping(value = "/user/grant", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserGrant(@RequestBody UserGrantDto userGrantDto){
        UserDbo userDbo = userService.grantUserRole(userGrantDto.getUserId(), userGrantDto.getGrantUserId(), userGrantDto.getRoleId());
        if (Objects.isNull(userDbo))
            return new APIResponseSet<>(false, "赋权失败");
        return new APIResponseSet<>(true, "赋权成功");
    }

    @PostMapping(value = "/user/password", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserUpdatePassword(@RequestBody UserDto userDto){
        if (Objects.isNull(userDto.getUserName())
                || Objects.isNull(userDto.getPassword()))
            return new APIResponseSet<>(false, "更新密码失败");
        UserDbo userDbo = userService.updateUserPassword(userDto.getUserName(), userDto.getPassword());
        if (Objects.isNull(userDbo))
            return new APIResponseSet<>(false, "更新密码失败");
        return new APIResponseSet<>(true, "更新密码成功");
    }

}
